<?php
/*
 * Created on 19 d�c. 2005
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once('Language.php');

function fill($pattern, $arg1, $arg2=-1, $arg3=-1,$arg4=-1, $arg5=-1, $arg6=-1)
{
	$match = $replace = array();

	for ( $i=0 ; $i<7 ; $i++ )
	{
		$value = ${"arg$i"};

		if ( $value!=-1 )
		{
			array_push($match, "%$i");
			array_push($replace, $value );
		}
	}
	
	return str_replace($match, $replace, $pattern);
}

function addSeparators($number)
{
	$reg = '([0-9])([0-9]{' . getTerm('others', 'separation_gap') . '})(([0-9]{' . getTerm('others', 'separation_gap') . '})*)$';
	$rep = '\1' . getTerm('others', 'separator') . '\2\3';

	do
	{
		$previous = $number;
		$number = ereg_replace($reg, $rep, $number);
	}
	while ( $previous!=$number );
	
	return $number;
}

function getTZOffset() { 
  $Offset = date("O", 0);
   
  $Parity = $Offset < 0 ? -1 : 1;
  $Offset = $Parity * $Offset;
  $Offset = ($Offset - ($Offset % 100))/100*60 + $Offset % 100;

  return $Parity * $Offset;
}

function gmtime($timestamp=-1, $is_associative=1)
{
	if ( $timestamp==-1 )
		$timestamp = time();
	
	$Offset = date("O", 0);
	
	$Parity = $Offset < 0 ? -1 : 1;
	$Offset = $Parity * $Offset;
	$Offset = ($Offset - ($Offset % 100))/100*60 + $Offset % 100;
  
	$TZOffset = $Parity * $Offset;
	
	$t_time = $timestamp-$TZOffset*60; #Counter adjust for localtime()

	return localtime($t_time, $is_associative);
}

function isBissextile($year)
{
	return ((($year % 4 == 0) && $year % 100 != 0) || $year % 400 == 0);
}

function lengthMonth($year, $month)
{
	if ($month==4 || $month==6 || $month==9 || $month==11)
		return 30;

	else if ( $month==2 && isBissextile($year))
		return 29;

	else if ($month==2)
		return 28;

	else
		return 31;
}

function dateToTimestamp($year, $month, $day)
{	
	$total = 0;

	for ($i=1970 ; $i<$year ; $i++)
		$total += isBissextile($i) ? 366 : 365;

	for ($i=1 ; $i<$month ; $i++)
		$total += lengthMonth($year,$i);

	$total += $day;

	return ($total * 86400) - 1;
}

function timeToTimestamp($hour, $minute, $second)
{
    return $second + $minute*60 + $hour*3600;
}

function formatDate($date)
{
	$matches = 0;

	if ( preg_match('/^([0-9]{4})-([0-9]{2})-([0-9]{2}) .*/i', $date, $matches) )

		$timestamp = dateToTimeStamp($matches[1], $matches[2], $matches[3]);
		
	else
	{
		$timestamp = $date;
		$values = gmtime($timestamp);
		$date = sprintf('%04d-%02d-%02d', $values['tm_year']+1900, $values['tm_mon']+1, $values['tm_mday']);
	}

	$today = time();
	
	if ( $today<=$timestamp && $today>$timestamp-86400 )

		return getTerm('time', 'today');
		
	elseif ( $today-86400<=$timestamp && $today>$timestamp )
	
		return getTerm('time', 'yesterday');
		
	elseif ( $today+86400<=$timestamp && $today+86400>$timestamp-86400 )
	
		return getTerm('time', 'tomorrow');
		
	else
	{
		$months = array('january','february','march','april','may','june','july','august','september','october','november','december');
		$days = array('sunday','monday','tuesday','wednesday','thursday','friday','saturday');
		
		$i = 0;

		do
		{
			$i++;

			$case = getTerm('time', "datecase$i");
			$pattern = getTerm('time', "datepattern$i");
			
			$case = '/^' . str_replace('*', '.*', $case) . '$/i';
		}
		while ( ! preg_match($case, $date) );
		
		$values = gmtime($timestamp);
		
		$find = array('%y', '%M', '%m', '%D', '%d');
		$replace = array($values['tm_year']+1900 ,
						getTerm('time', $months[$values['tm_mon']]),
						$values['tm_month']+1,
						getTerm('time', $days[$values['tm_wday']]),
						$values['tm_mday'] );
		
		return str_replace($find, $replace, $pattern);
	}
}

function formatTime($time)
{
	if ( preg_match('/([0-9]{2}):([0-9]{2}):([0-9]{2})$/i', $time, $matches) )
		$timestamp = timeToTimestamp($matches[1], $matches[2], $matches[3]);
		
	else
	{
		$timestamp = $time;
		$values = gmtime($timestamp);
		$time = sprintf('%02d:%02d:%02d', $values['tm_year']+1900, $values['tm_mon']+1, $values['tm_mday']);
    }
		
	
	$i = 0;

	do
	{
		$i++;

		$case = getTerm('time', "timecase$i");
		$pattern = getTerm('time', "timepattern$i");
			
		$case = '/^' . str_replace('*', '.*', $case) . '$/i';
	}
	while ( ! preg_match($case, $time) );
		
    $values = gmtime($timestamp);
		
	$find = array('%h', '%H', '%m', '%s', '%A');
	$replace = array(  $values['tm_hour'] ,
	                   $values['tm_hour']>12 ? $values['tm_hour']-12 : $values['tm_hour'] ,
					   sprintf('%02d', $values['tm_min']) ,
				       sprintf('%02d', $values['tm_sec']) ,
				       $values['tm_hour']>12 ? getTerm('time','pm') : getTerm('time','am') );
		
	return str_replace($find, $replace, $pattern);
}

?>
